<?
if (preg_match("#" . basename(__FILE__) . "#", $_SERVER['PHP_SELF'])) {
    die('This page cannot be called directly.');
}

require_once 'includes/AjaxRes.php'; //System Debug...
class ajax_deal
{
    function playme()
    {
        $code = Url::getParam('code');
        //yêu cầu đặt tên $code=tên của method trong class

        if (method_exists($this, $code)) {
            $this->$code();
        } else {
            $this->home();
        }
    }

    function home()
    {
        global $display;
        die("Nothing to do 1212...");
    }

    function get_available_room()
    {
        $room_id = Url::get('room_id', 0);
        $checkin = strtotime(Url::get('checkin', 0));
        $checkout = strtotime(Url::get('checkout', 0));
        if ($room_id) {
            $total_room = Db_Room::getMinAvailableRoomNumber($room_id, $checkin, $checkout);
            echo $total_room;
            return;
        } else {
            echo 0;
            return;
        }
    }

    function cart()
    {
        global $display;
        $ajax_response = new AjaxRes();

        //thông tin user
        if (User::is_login()) {
            $user = DB::select("user", "id=" . User::id());

            //Get info from cookies if exist
            if (isset($_COOKIE["cus_name"]) && isset($_COOKIE["cus_phone"]) && isset($_COOKIE["cus_email"])) {
                if ($_COOKIE["cus_name"] != "" && $_COOKIE["cus_phone"] != "" && $_COOKIE["cus_email"] != "") {
                    $display->add('name', $_COOKIE["cus_name"]);
                    $display->add('phone', $_COOKIE["cus_phone"]);
                    $display->add('email', $_COOKIE["cus_email"]);
                    if (isset($_COOKIE["cus_note"])) {
                        $display->add('note', $_COOKIE["cus_note"]);
                    }
                    if (isset($_COOKIE["cus_cmnd"])) {
                        $display->add('cmnd', $_COOKIE["cus_cmnd"]);
                    }
                } else {
                    $display->add('name', $user['full_name']);
                    $display->add('phone', $user['mobile_phone']);
                    $display->add('email', $user['email']);
                    $display->add('note', "");
                }
            } else {
                $display->add('name', $user['full_name']);
                $display->add('phone', $user['mobile_phone']);
                $display->add('email', $user['email']);
                $display->add('note', "");
            }

            //danh bạ
            $contacts = DB::select_all('cart_contact', 'user_id=' . $user['id']);
            $display->add('contacts', $contacts);
            $display->add('numcontacts', sizeof($contacts));

            //tính tiền
            $checkin = Url::get('checkin', '');
            $deal_id = Url::get('deal_id', 0);
            $quantity = Url::get("quantity", 0);

            if ($checkin == '' || !$deal_id || !$quantity) {
                $ajax_response->type = AjaxRes::ERROR;
                echo $ajax_response->toString();
                return;
            } else {
                $checkin = strtotime($checkin);
                $deal = Db_Deal::getDealHotelById($deal_id);
                $checkout = $checkin + 24 * 60 * 60 * $deal['num_night'];
                $display->add('checkin', date("d-m-Y ", $checkin));
                $display->add('checkout', date("d-m-Y ", $checkout));
                $display->add('quantity', $quantity);
                $price = $deal['price'] * $quantity;
                $display->add('price', $price);
                $display->add('deal', $deal);
            }
            $display->add('WEB_NAME', WEB_NAME);
            $item = $display->output('processcart', true, 'ProcessCart');
            $ajax_response->type = AjaxRes::SUCCESS;
            $ajax_response->item = $item;
            echo $ajax_response->toString();
            return;
        } else {
            $ajax_response->type = AjaxRes::WARNING;
            echo $ajax_response->toString();
        }
    }

    function validate()
    {
        //thông tin đơn hàng
        $deal_id = Url::get("deal_id", 0);
        $checkin = strtotime(Url::get('checkin', ''));
        $checkout = strtotime(Url::get('checkout', ''));
        $quantity = Url::get("quantity", 0);
        $cus = Url::get("cus");
        $type_payment = Url::get("type");

        $price = 0;
        $total_room = 0;

        if(!$deal_id){
            echo 'wrong_deal';
            exit;
        }
        $deal = Db_Deal::getDealHotelById($deal_id);
        if(!$deal){
            echo 'wrong_deal';
            exit;
        }
        if ($type_payment == '' || $type_payment == 'undefined') {
            echo 'wrong_type_payment';
            exit;
        }
        if ($checkin == '' || $checkout == '') {
            echo 'wrong_date';
            exit;
        } else {
//            var_dump($deal);exit();
            if($deal['num_deal'] == 0){
                //không giới hạn số deal
            }else{
                //giới hạn số deal
            }

        }

        //thông tin khách hàng
        if (User::is_login()) {
            $user_new = array();
            $user_new['name'] = $cus[0];
            $user_new['organization'] = $cus[5];
            $user_new['phone'] = $cus[1];
            $user_new['email'] = $cus[2];
            $user_new['address'] = $cus[4];
            $user_new['location_id'] = $cus[6];
            $user_new['note'] = $cus[3];
            $user_new['cmnd'] = $cus[7];

            //Set cookie lưu lại thông tin KH
            $time = 60 * 60 * 24 * 365 + TIME_NOW;
            setcookie("cus_name", $user_new['name'], $time, "/");
            setcookie("cus_phone", $user_new['phone'], $time, "/");
            setcookie("cus_email", $user_new['email'], $time, "/");
            setcookie("cus_note", $user_new['note'], $time, "/");
            setcookie("cus_cmnd", $user_new['cmnd'], $time, "/");
        }

        $price = $deal['price'] * $quantity;
        $cart = array(
            "deal_id" => $deal_id,
            "total_room" => $quantity,
            "int_from_date" => $checkin,
            "int_to_date" => $checkout,
            "total" => $price,
            "type_payment" => $type_payment,
            "user" => $user_new
        );

        $_SESSION['cart'] = $cart;

        echo 'success';
    }
}

?>